package jsqlite;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import org.codehaus.jackson.util.MinimalPrettyPrinter;

/* loaded from: classes2.dex */
public class Shell implements Callback {
    static final int MODE_Column = 1;
    static final int MODE_Html = 4;
    static final int MODE_Insert = 5;
    static final int MODE_Insert2 = 6;
    static final int MODE_Line = 0;
    static final int MODE_List = 2;
    static final int MODE_Semi = 3;
    String[] cols;
    int[] colwidth;
    int count;
    Database db;
    String destTable;
    boolean echo;
    PrintWriter err;
    int mode;
    PrintWriter pw;
    String sep;
    boolean showHeader;
    String tableName;

    @Override // jsqlite.Callback
    public void types(String[] strArr) {
    }

    public Shell(PrintWriter printWriter, PrintWriter printWriter2) {
        this.pw = printWriter;
        this.err = printWriter2;
    }

    public Shell(PrintStream printStream, PrintStream printStream2) {
        this.pw = new PrintWriter(printStream);
        this.err = new PrintWriter(printStream2);
    }

    public Object clone() {
        Shell shell = new Shell(this.pw, this.err);
        shell.db = this.db;
        shell.echo = this.echo;
        shell.mode = this.mode;
        shell.count = 0;
        shell.showHeader = this.showHeader;
        shell.tableName = this.tableName;
        shell.sep = this.sep;
        shell.colwidth = this.colwidth;
        return shell;
    }

    public static String sql_quote_dbl(String str) {
        if (str == null) {
            return "NULL";
        }
        int i = 0;
        for (int i2 = 0; i2 < str.length(); i2++) {
            if (str.charAt(i2) != '\'' && str.charAt(i2) == '\"') {
                i++;
            }
        }
        if (i == 0) {
            return "\"" + str + "\"";
        }
        StringBuffer stringBuffer = new StringBuffer("\"");
        for (int i3 = 0; i3 < str.length(); i3++) {
            char charAt = str.charAt(i3);
            if (charAt == '\"') {
                stringBuffer.append("\"\"");
            } else {
                stringBuffer.append(charAt);
            }
        }
        return stringBuffer.toString();
    }

    public static String sql_quote(String str) {
        if (str == null) {
            return "NULL";
        }
        int i = 0;
        int i2 = 0;
        for (int i3 = 0; i3 < str.length(); i3++) {
            if (str.charAt(i3) == '\'') {
                i++;
            } else if (str.charAt(i3) == '\"') {
                i2++;
            }
        }
        if (i == 0) {
            return "'" + str + "'";
        } else if (i2 == 0) {
            return "\"" + str + "\"";
        } else {
            StringBuffer stringBuffer = new StringBuffer("'");
            for (int i4 = 0; i4 < str.length(); i4++) {
                char charAt = str.charAt(i4);
                if (charAt == '\'') {
                    stringBuffer.append("''");
                } else {
                    stringBuffer.append(charAt);
                }
            }
            return stringBuffer.toString();
        }
    }

    static String html_quote(String str) {
        if (str == null) {
            return "NULL";
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            char charAt = str.charAt(i);
            if (charAt == '<') {
                stringBuffer.append("&lt;");
            } else if (charAt == '>') {
                stringBuffer.append("&gt;");
            } else if (charAt == '&') {
                stringBuffer.append("&amp;");
            } else if (charAt < ' ' || charAt > 127) {
                stringBuffer.append("&#" + ((int) charAt) + ";");
            } else {
                stringBuffer.append(charAt);
            }
        }
        return stringBuffer.toString();
    }

    static boolean is_numeric(String str) {
        try {
            Double.valueOf(str);
            return true;
        } catch (java.lang.Exception unused) {
            return false;
        }
    }

    public void set_table_name(String str) {
        if (str == null) {
            this.tableName = "";
        } else if (this.db.is3()) {
            this.tableName = sql_quote_dbl(str);
        } else {
            this.tableName = sql_quote(str);
        }
    }

    @Override // jsqlite.Callback
    public void columns(String[] strArr) {
        this.cols = strArr;
    }

    @Override // jsqlite.Callback
    public boolean newrow(String[] strArr) {
        switch (this.mode) {
            case 0:
                if (strArr.length != 0) {
                    int i = this.count;
                    this.count = i + 1;
                    if (i > 0) {
                        this.pw.println("");
                    }
                    for (int i2 = 0; i2 < strArr.length; i2++) {
                        PrintWriter printWriter = this.pw;
                        StringBuilder sb = new StringBuilder(String.valueOf(this.cols[i2]));
                        sb.append(" = ");
                        sb.append(strArr[i2]);
                        printWriter.println(sb.toString() == null ? "NULL" : strArr[i2]);
                    }
                    break;
                }
                break;
            case 1:
                int i3 = this.count;
                this.count = i3 + 1;
                if (i3 == 0) {
                    this.colwidth = new int[strArr.length];
                    String str = "";
                    for (int i4 = 0; i4 < strArr.length; i4++) {
                        int length = this.cols[i4].length();
                        if (length < 10) {
                            length = 10;
                        }
                        this.colwidth[i4] = length;
                        if (this.showHeader) {
                            PrintWriter printWriter2 = this.pw;
                            printWriter2.print(str + this.cols[i4]);
                            str = MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR;
                        }
                    }
                    if (this.showHeader) {
                        this.pw.println("");
                    }
                }
                if (strArr.length != 0) {
                    String str2 = "";
                    int i5 = 0;
                    while (i5 < strArr.length) {
                        PrintWriter printWriter3 = this.pw;
                        StringBuilder sb2 = new StringBuilder(str2);
                        sb2.append(strArr[i5] == null ? "NULL" : strArr[i5]);
                        printWriter3.print(sb2.toString());
                        i5++;
                        str2 = MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR;
                    }
                    this.pw.println("");
                    break;
                }
                break;
            case 2:
            case 3:
                int i6 = this.count;
                this.count = i6 + 1;
                if (i6 == 0 && this.showHeader) {
                    int i7 = 0;
                    while (i7 < strArr.length) {
                        PrintWriter printWriter4 = this.pw;
                        StringBuilder sb3 = new StringBuilder(String.valueOf(this.cols[i7]));
                        sb3.append(i7 == strArr.length + (-1) ? "\n" : this.sep);
                        printWriter4.print(sb3.toString());
                        i7++;
                    }
                }
                if (strArr.length != 0) {
                    for (int i8 = 0; i8 < strArr.length; i8++) {
                        this.pw.print(strArr[i8] == null ? "NULL" : strArr[i8]);
                        if (this.mode == 3) {
                            this.pw.print(";");
                        } else if (i8 < strArr.length - 1) {
                            this.pw.print(this.sep);
                        }
                    }
                    this.pw.println("");
                    break;
                }
                break;
            case 4:
                int i9 = this.count;
                this.count = i9 + 1;
                if (i9 == 0 && this.showHeader) {
                    this.pw.print("<TR>");
                    for (int i10 = 0; i10 < strArr.length; i10++) {
                        PrintWriter printWriter5 = this.pw;
                        printWriter5.print("<TH>" + html_quote(this.cols[i10]) + "</TH>");
                    }
                    this.pw.println("</TR>");
                }
                if (strArr.length != 0) {
                    this.pw.print("<TR>");
                    for (int i11 = 0; i11 < strArr.length; i11++) {
                        PrintWriter printWriter6 = this.pw;
                        printWriter6.print("<TD>" + html_quote(strArr[i11]) + "</TD>");
                    }
                    this.pw.println("</TR>");
                    break;
                }
                break;
            case 5:
                if (strArr.length != 0) {
                    String str3 = this.tableName;
                    String str4 = this.destTable;
                    if (str4 != null) {
                        str3 = str4;
                    }
                    PrintWriter printWriter7 = this.pw;
                    printWriter7.print("INSERT INTO " + str3 + " VALUES(");
                    int i12 = 0;
                    while (i12 < strArr.length) {
                        String str5 = i12 > 0 ? "," : "";
                        if (strArr[i12] == null) {
                            PrintWriter printWriter8 = this.pw;
                            printWriter8.print(str5 + "NULL");
                        } else if (is_numeric(strArr[i12])) {
                            PrintWriter printWriter9 = this.pw;
                            printWriter9.print(str5 + strArr[i12]);
                        } else {
                            PrintWriter printWriter10 = this.pw;
                            printWriter10.print(str5 + sql_quote(strArr[i12]));
                        }
                        i12++;
                    }
                    this.pw.println(");");
                    break;
                }
                break;
            case 6:
                if (strArr.length != 0) {
                    String str6 = this.tableName;
                    String str7 = this.destTable;
                    if (str7 != null) {
                        str6 = str7;
                    }
                    PrintWriter printWriter11 = this.pw;
                    printWriter11.print("INSERT INTO " + str6 + " VALUES(");
                    int i13 = 0;
                    while (i13 < strArr.length) {
                        String str8 = i13 > 0 ? "," : "";
                        PrintWriter printWriter12 = this.pw;
                        printWriter12.print(str8 + strArr[i13]);
                        i13++;
                    }
                    this.pw.println(");");
                    break;
                }
                break;
        }
        return false;
    }

    void do_meta(String str) {
        StringTokenizer stringTokenizer = new StringTokenizer(str.toLowerCase());
        int countTokens = stringTokenizer.countTokens();
        if (countTokens <= 0) {
            return;
        }
        String nextToken = stringTokenizer.nextToken();
        int i = countTokens - 1;
        String[] strArr = new String[i];
        int i2 = 0;
        while (stringTokenizer.hasMoreTokens()) {
            strArr[i2] = stringTokenizer.nextToken();
            i2++;
        }
        if (nextToken.compareTo(".dump") == 0) {
            new DBDump(this, strArr);
        } else if (nextToken.compareTo(".echo") == 0) {
            if (i > 0) {
                if (strArr[0].startsWith("y") || strArr[0].startsWith("on")) {
                    this.echo = true;
                }
            }
        } else {
            if (nextToken.compareTo(".exit") == 0) {
                try {
                    this.db.close();
                } catch (Exception unused) {
                }
                System.exit(0);
            }
            if (nextToken.compareTo(".header") == 0) {
                if (i > 0) {
                    if (strArr[0].startsWith("y") || strArr[0].startsWith("on")) {
                        this.showHeader = true;
                    }
                }
            } else if (nextToken.compareTo(".help") == 0) {
                this.pw.println(".dump ?TABLE? ...  Dump database in text fmt");
                this.pw.println(".echo ON|OFF       Command echo on or off");
                this.pw.println(".enc ?NAME?        Change encoding");
                this.pw.println(".exit              Exit program");
                this.pw.println(".header ON|OFF     Display headers on or off");
                this.pw.println(".help              This message");
                this.pw.println(".mode MODE         Set output mode to\n                   line, column, insert\n                   list, or html");
                this.pw.println(".mode insert TABLE Generate SQL insert stmts");
                this.pw.println(".schema ?PATTERN?  List table schema");
                this.pw.println(".separator STRING  Set separator string");
                this.pw.println(".tables ?PATTERN?  List table names");
            } else if (nextToken.compareTo(".mode") == 0) {
                if (i > 0) {
                    if (strArr[0].compareTo("line") == 0) {
                        this.mode = 0;
                    } else if (strArr[0].compareTo("column") == 0) {
                        this.mode = 1;
                    } else if (strArr[0].compareTo("list") == 0) {
                        this.mode = 2;
                    } else if (strArr[0].compareTo("html") == 0) {
                        this.mode = 4;
                    } else if (strArr[0].compareTo("insert") == 0) {
                        this.mode = 5;
                        if (i > 1) {
                            this.destTable = strArr[1];
                        }
                    }
                }
            } else if (nextToken.compareTo(".separator") == 0) {
                if (i > 0) {
                    this.sep = strArr[0];
                }
            } else {
                r5 = null;
                TableResult tableResult = null;
                if (nextToken.compareTo(".tables") == 0) {
                    if (i > 0) {
                        try {
                            tableResult = this.db.get_table("SELECT name FROM sqlite_master WHERE type='table' AND name LIKE '%%%q%%' ORDER BY name", new String[]{strArr[0]});
                        } catch (Exception e) {
                            PrintWriter printWriter = this.err;
                            printWriter.println("SQL Error: " + e);
                            this.err.flush();
                        }
                    } else {
                        try {
                            tableResult = this.db.get_table("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name");
                        } catch (Exception e2) {
                            PrintWriter printWriter2 = this.err;
                            printWriter2.println("SQL Error: " + e2);
                            this.err.flush();
                        }
                    }
                    if (tableResult != null) {
                        for (int i3 = 0; i3 < tableResult.nrows; i3++) {
                            String str2 = ((String[]) tableResult.rows.elementAt(i3))[0];
                            if (str2 != null) {
                                this.pw.println(str2);
                            }
                        }
                    }
                } else if (nextToken.compareTo(".schema") == 0) {
                    if (i > 0) {
                        try {
                            this.db.exec("SELECT sql FROM sqlite_master WHERE type!='meta' AND name LIKE '%%%q%%' AND sql NOTNULL ORDER BY type DESC, name", this, new String[]{strArr[0]});
                            return;
                        } catch (Exception e3) {
                            PrintWriter printWriter3 = this.err;
                            printWriter3.println("SQL Error: " + e3);
                            this.err.flush();
                            return;
                        }
                    }
                    try {
                        this.db.exec("SELECT sql FROM sqlite_master WHERE type!='meta' AND sql NOTNULL ORDER BY tbl_name, type DESC, name", this);
                    } catch (Exception e4) {
                        PrintWriter printWriter4 = this.err;
                        printWriter4.println("SQL Error: " + e4);
                        this.err.flush();
                    }
                } else if (nextToken.compareTo(".enc") == 0) {
                    try {
                        this.db.set_encoding(i > 0 ? strArr[0] : null);
                    } catch (Exception e5) {
                        PrintWriter printWriter5 = this.err;
                        StringBuilder sb = new StringBuilder();
                        sb.append(e5);
                        printWriter5.println(sb.toString());
                        this.err.flush();
                    }
                } else if (nextToken.compareTo(".rekey") == 0) {
                    try {
                        this.db.rekey(i > 0 ? strArr[0] : null);
                    } catch (Exception e6) {
                        PrintWriter printWriter6 = this.err;
                        StringBuilder sb2 = new StringBuilder();
                        sb2.append(e6);
                        printWriter6.println(sb2.toString());
                        this.err.flush();
                    }
                } else {
                    PrintWriter printWriter7 = this.err;
                    printWriter7.println("Unknown command '" + nextToken + "'");
                    this.err.flush();
                }
            }
        }
    }

    String read_line(BufferedReader bufferedReader, String str) {
        if (str != null) {
            try {
                this.pw.print(str);
                this.pw.flush();
            } catch (IOException unused) {
                return null;
            }
        }
        return bufferedReader.readLine();
    }

    void do_input(BufferedReader bufferedReader) {
        String str = "SQLITE> ";
        String str2 = null;
        while (true) {
            String read_line = read_line(bufferedReader, str);
            if (read_line == null) {
                break;
            }
            if (this.echo) {
                this.pw.println(read_line);
            }
            if (read_line.length() > 0 && read_line.charAt(0) == '.') {
                do_meta(read_line);
            } else {
                if (str2 == null) {
                    str2 = read_line;
                } else {
                    str2 = String.valueOf(str2) + MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR + read_line;
                }
                if (Database.complete(str2)) {
                    try {
                        this.db.exec(str2, this);
                    } catch (Exception e) {
                        if (!this.echo) {
                            this.err.println(str2);
                        }
                        this.err.println("SQL Error: " + e);
                        this.err.flush();
                    }
                    str = "SQLITE> ";
                    str2 = null;
                } else {
                    str = "SQLITE? ";
                }
            }
            this.pw.flush();
        }
        if (str2 != null) {
            this.err.println("Incomplete SQL: " + str2);
            this.err.flush();
        }
    }

    void do_cmd(String str) {
        if (this.db == null) {
            return;
        }
        if (str.length() > 0 && str.charAt(0) == '.') {
            do_meta(str);
            return;
        }
        try {
            this.db.exec(str, this);
        } catch (Exception e) {
            PrintWriter printWriter = this.err;
            printWriter.println("SQL Error: " + e);
            this.err.flush();
        }
    }

    public static void main(String[] strArr) {
        Shell shell = new Shell(System.out, System.err);
        shell.mode = 2;
        shell.sep = "|";
        shell.showHeader = false;
        shell.db = new Database();
        String str = null;
        String str2 = null;
        String str3 = null;
        int i = 0;
        while (i < strArr.length) {
            if (strArr[i].compareTo("-html") == 0) {
                shell.mode = 4;
            } else if (strArr[i].compareTo("-list") == 0) {
                shell.mode = 2;
            } else if (strArr[i].compareTo("-line") == 0) {
                shell.mode = 0;
            } else if (i < strArr.length - 1 && strArr[i].compareTo("-separator") == 0) {
                i++;
                shell.sep = strArr[i];
            } else if (strArr[i].compareTo("-header") == 0) {
                shell.showHeader = true;
            } else if (strArr[i].compareTo("-noheader") == 0) {
                shell.showHeader = false;
            } else if (strArr[i].compareTo("-echo") == 0) {
                shell.echo = true;
            } else if (strArr[i].compareTo("-key") == 0) {
                i++;
                str2 = strArr[i];
            } else if (str == null) {
                str = strArr[i];
            } else if (str3 == null) {
                str3 = strArr[i];
            } else {
                System.err.println("Arguments: ?OPTIONS? FILENAME ?SQL?");
                System.exit(1);
            }
            i++;
        }
        if (str == null) {
            System.err.println("No database file given");
            System.exit(1);
        }
        try {
            shell.db.open(str, 6);
        } catch (Exception e) {
            System.err.println("Unable to open database: " + e);
            System.exit(1);
        }
        if (str2 != null) {
            try {
                shell.db.key(str2);
            } catch (Exception e2) {
                System.err.println("Unable to set key: " + e2);
                System.exit(1);
            }
        }
        if (str3 != null) {
            shell.do_cmd(str3);
            shell.pw.flush();
        } else {
            shell.do_input(new BufferedReader(new InputStreamReader(System.in)));
            shell.pw.flush();
        }
        try {
            shell.db.close();
        } catch (Exception unused) {
        }
    }
}
